//
//  SEGSHManager.m
//  ExcellentHome
//
//  Created by 刘山 on 16/9/5.
//  Copyright © 2016年 陈区. All rights reserved.
//
#import "SEGSHManager.h"
#import "SEGHTTPBusinessManager.h"
#import "SEGProfileManager.h"
#import "SEGUtils.h"
#import "SEGAnalyseBehaviorManager.h"
#import "SEGFindDictionaryInfoManagr.h"

#if __has_include(<SEGPlatform/SEGBussinessConstHeader.h>)
#import <SEGPlatform/SEGBussinessConstHeader.h>
#else
#import "SEGBussinessConstHeader.h"
#endif

#import "NSObject+SEGModel.h"

@implementation SEGSHManager

/**
 更新二手集市状态（删除或出手）
 
 @param goodsId goodsId
 @param objType objType
 @param status 删除传3 出手传2
 */
- (void)requestUpdateStatusBygoodsId:(NSString*)goodsId objType:(NSString*)objType status:(NSString*)status Finished:(SEGRequestCallBack)finished
{
    NSDictionary* params = @{
        @"goodsId" : goodsId,
        @"status" : status
    };
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/ecomm/goodsIdel/updateStatus.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        NSDictionary *notificationObject = @{@"objId":ValueToString(goodsId), @"objType":ValueToString(objType)};
        if(isSuccessed && [status isEqualToString:@"3"]) {//删除集市
            //删除成功发送通知刷新界面
            [[NSNotificationCenter defaultCenter] postNotificationName:SEGDeletaFinishedNotification object:notificationObject];
            //请求积分行为接口
            [[SEGAnalyseBehaviorManager sharedManager] requestIntegralGrowthWithCode:SEGIntegralCode.fornumIsDeleted finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            }];
        }
        if (isSuccessed && [status isEqualToString:@"2"]) { //集市出手
            //出手成功发送通知刷新界面
            [[NSNotificationCenter defaultCenter] postNotificationName:SEGMarketSaleFinishedNotification object:notificationObject];
        }
        finished(isSuccessed, codeType, errorMessage);
    }];
}

-(NSString*)statusNameWithType:(SEGEnumIdelExchange)type status:(SEGEnumIdleStatus)status
{
    switch (type) {
        case SEGEnumIdelExchange_Business:
        {
            switch (status) {
                case SEGEnumIdleStatus_Idle:
                {
                    return kIdle;
                }
                    break;
                case SEGEnumIdleStatus_SaleOut:
                {
                    return kSaleOut;
                }
                    break;
                case SEGEnumIdleStatus_Remove:
                {}
                    break;
            }
        }
            break;
        case SEGEnumIdelExchange_SecondHand:
        {
            switch (status) {
                case SEGEnumIdleStatus_Idle:
                {
                    return kIdle;
                }
                    break;
                case SEGEnumIdleStatus_SaleOut:
                {
                    return kSaleOut;
                }
                    break;
                case SEGEnumIdleStatus_Remove:
                {}
                    break;
            }
        }
            break;
        case SEGEnumIdelExchange_Gift:
        {
            switch (status) {
                case SEGEnumIdleStatus_Idle:
                {
                    return kIdle;
                }
                    break;
                case SEGEnumIdleStatus_SaleOut:
                {
                    return kSaleOut;
                }
                    break;
                case SEGEnumIdleStatus_Remove:
                {}
                    break;
            }
        }
            break;
        case SEGEnumIdelExchange_Lend:
        {
            switch (status) {
                case SEGEnumIdleStatus_Idle:
                {
                    return kIdle;
                }
                    break;
                case SEGEnumIdleStatus_SaleOut:
                {
                    return kSaleOut;
                }
                    break;
                case SEGEnumIdleStatus_Remove:
                {}
                    break;
            }
        }
            break;
        default:
            return @"";
    }
    return @"";
}

- (void)requestGetGoodsIdelBygoodsId:(NSString*)goodsId Finished:(SEGRequestCallBack)finished
{
    NSDictionary* params = @{
        @"goodsId" : goodsId
    };
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/ecomm/goodsIdel/getGoodsIdel.json",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary* dicData = [result valueForKey:@"data"];
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(NO, codeType, errorMessage);
            return;
        }
        NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithDictionary:dicData];
        [temp setObject:@"1" forKey:@"isDetailView"];//是详情页
        self.dataSourceGoodsIdel = [SEGQueryGoodsIdelModel initWithDictionary:temp];
        
        finished(YES, codeType, errorMessage);
        
    }];
}

/**
 * 请求提交闲置信息
 * @author Wendy
 * @param params 参数字典
 * @param finished 回调结果
 */
- (void)requestSubmitGoodsIdelWithParams:(NSDictionary *)params finished:(SEGNetworkReponse)finished
{
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/ecomm/goodsIdel/submitGoodsIdel.json",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, nil, codeType, errorMessage);
            return;
        }
        
        finished(YES, result, codeType, errorMessage);
        
    }];
}

/**
 * 请求押金单位
 * @author Wendy
 * @param finished 回调结果
 */
- (void)requestRentUnit:(SEGNetworkReponse)finished
{
    [[SEGFindDictionaryInfoManagr sharedManager] requestDictionaryInfoByCode:@"ECOMM_GOODS_IDEL_RENT_CODE" completed:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
        if (!isSuccessed) {
            finished(NO, nil, codeType, errorMessage);
            return;
        }
        NSDictionary* dicData = [result valueForKey:@"data"];
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(NO, nil, codeType, errorMessage);
            return;
        }
        finished(isSuccessed, result, codeType, errorMessage);
    }];
}


/**
 * 请求商品分类
 * @author Wendy
 * @param classifyId 大分类ID
 * @param finished 回调结果
 */
- (void)requestGoodsClassifyWithClassifyId:(NSString *)classifyId finished:(SEGNetworkReponse)finished
{
    NSDictionary *params = @{@"classifyId":classifyId};

    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/bms-api/bms/classify/api/classify",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, nil, codeType, errorMessage);
            return;
        }

        finished(YES, result, codeType, errorMessage);
        
    }];
}

/**
 查询单条跳蚤市场数据
 
 @param goodsId 数据ID
 @param objType 类型
 @param finished 回调
 */
- (void)requestQueryMarketSingleWithGoodsId:(NSString *)goodsId
                                    objType:(NSString *)objType
                                   finished:(SEGRequestCallBack)finished {
    NSDictionary *params = @{@"goodsId":goodsId,
                             @"objType":objType};
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/ugc-pgc/api/v4/queryMarketSingle",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        NSDictionary* dicData = [result valueForKey:@"data"];
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(NO, codeType, errorMessage);
            return;
        }
        self.replaceModel = [SEGQueryGoodsIdelModel initWithDictionary:dicData];
        finished(YES, codeType, errorMessage);
    }];
}


/// 查询话题列表
/// @param finished 回调
- (void)requestTopicListWithPage:(NSString *)page finished:(SEGRequestCallBack)finished {
    NSDictionary *params = @{@"pageNo":page,@"pageSize":@"15"};
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/api/v2/topicList",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        NSDictionary* dicData = [result valueForKey:@"data"];
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(NO, codeType, errorMessage);
            return;
        }
        self.topicDataModel = [SEGTopicDataModel yy_modelWithDictionary:dicData];
        finished(YES, codeType, errorMessage);
    }];
}


- (NSMutableArray<SEGQueryGoodsIdelModel*>*)dataSourceQueryGoodsIdel
{
    if (!_dataSourceQueryGoodsIdel) {

        _dataSourceQueryGoodsIdel = [NSMutableArray array];
    }
    return _dataSourceQueryGoodsIdel;
}

- (SEGQueryGoodsIdelModel*)dataSourceGoodsIdel
{
    if (!_dataSourceGoodsIdel) {
        _dataSourceGoodsIdel = [[SEGQueryGoodsIdelModel alloc] init];
    }
    return _dataSourceGoodsIdel;
}


@end
